/////////////////////////////////////////////////////////////////////////////
// Copyright(c) 2012-2015 purang All Rights Reserved
// Name:        socket_wrapper.h
// Purpose:
// Author:      syy
// Modified by:
// Created:     2014/8/28 13:18
// RCS-ID:
// Licence:
/////////////////////////////////////////////////////////////////////////////
#ifndef _SOCKET_WRAPPER_H_
#define _SOCKET_WRAPPER_H_
#include <string>

#include "../include/qbnet.h"
#include "core/common_types.h"

namespace qb {
class S_QBNET_EXPORT CSocketWrapper {
 public:
  static int32_t GetSockError();
  CSocketWrapper();
  virtual ~CSocketWrapper();
  int32_t Create(uint16_t socket_type);
  int32_t Close();
  int32_t SetOpt();
  int32_t SetTcpOpt();
  int32_t Connect(const char* ipaddress, uint16_t port);
  int32_t SetKeepalive();
  int32_t BindRandPort(uint16_t& local_port);
  int32_t GetFirstIPAddress(std::string& local_ipaddress);
  int32_t GetIPAddrInfo(const char* ipaddress, uint16_t port);
#if _WIN32
  uint32_t GetSocket();
#else
  int32_t GetSocket();
#endif
  int32_t SendTo(const char* buff, uint32_t len);
  int32_t Send(const char* buff, uint32_t len);
  int32_t Select();
  int32_t Recv(char* buff, uint32_t len);
  int32_t Recvfrom(char* buff, uint32_t len, void* props);
  bool IsInvalid();
  inline bool IsClosed();

  uint32_t GetTargetIP();
  uint16_t GetTargetPort();
  bool CompareAddrInfo(void* addr_info);

 private:
  int32_t SetNBIO();
  int32_t SetBIO(int32_t mode);

 private:
  bool m_closed;
  uint32_t m_uTargetIP;
  uint16_t m_uTargetPort;
#if _WIN32
  uint32_t m_socket;
#else
  int32_t m_socket;
#endif
  void* m_addr_info;
};
}  // namespace qb

#endif  // _SOCKET_WRAPPER_H_
